package com.movitech.mobile.cz.base.supplier.bizcontroller;

import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.excel.ExportExcel;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.supplier.bizservice.SSuppliersEvaluationService;
import com.movitech.mobile.cz.base.supplier.bizvo.*;
import com.movitech.mobile.cz.base.supplier.entity.SEvaluationTemplate;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliers;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliersMbsContacts;
import com.movitech.mobile.cz.base.supplier.entity.ext.*;
import com.movitech.mobile.cz.base.supplier.service.SHistoryCommentService;
import com.movitech.mobile.cz.base.supplier.service.SSuppliersRankService;
import com.movitech.mobile.cz.base.supplier.service.SSuppliersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * SSuppliers 供方评估管理 前端控制器
 *
 * @author Blue.Xia
 * @since 2018-03-06
 */
@RestController
@RequestMapping("/v1/sSuppliersEvaluation")
@Api(value = "供方评估管理接口", description = "供方评估管理接口")
public class SSuppliersEvaluationController {

    @Autowired
    private SSuppliersEvaluationService sSuppliersEvaluationService;

    @Autowired
    private SSuppliersRankService sSuppliersRankService;

    @Autowired
    private SHistoryCommentService sHistoryCommentService;
    @Autowired
    private SSuppliersService sSuppliersService;


//    @PostMapping("/listEvaluation")
//    @ApiOperation(value = "供方信息列表接口", notes = "供方信息列表接口")
//    public RestReturnResult<Object> listEvaluation(@RequestBody EvaluationListRequestVO evaluationListRequestVO) {
//        RestReturnResult<Object> restResult = RestReturnResult.success();
//        PageCode pageCode = sSuppliersEvaluationService.findEvaluationList(evaluationListRequestVO);
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("list", pageCode);
//        restResult.setData(map);
//        restResult.setCode(CommonConstants.SUCCESS);
//        return restResult;
//    }
    
    @PostMapping("/searchEvaluationList")
    @ApiOperation(value = "评估列表(履约过程、履约后列表)", notes = "供方评估")
    public RestReturnResult searchEvaluationList(@RequestBody EvaluationListReqVO evaluationListReqVO){
    	RestReturnResult restReturnResult = RestReturnResult.success();
    	restReturnResult = sSuppliersEvaluationService.searchEvaluationList(evaluationListReqVO);
    	return restReturnResult;
    }

    @PostMapping("/exportEvaluationList")
    @ApiOperation(value = "评估列表(履约过程、履约后列表)", notes = "评估列表(履约过程、履约后列表)")
    public void exportSpecialPurchaseDetail(
            @RequestBody  EvaluationListReqVO reqVO, HttpServletResponse response, HttpServletRequest request){
        List<ExtSEvaluation> sEvaluations = sSuppliersEvaluationService.exportEvaluationList(reqVO);
        String fileName ="评估列表.xlsx";
        try {
            new ExportExcel("评估列表", ExtSEvaluation.class)
                    .setDataListSum(sEvaluations)
                    .write(request,response, fileName).dispose();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getSEvaluationById/{evaluationId}")
    @ApiOperation(value = "供方评估详情接口", notes = "供方评估详情取接口")
    public RestReturnResult<Object> getSEvaluationById(@PathVariable String evaluationId) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        ExtSEvaluation sEvaluation = sSuppliersEvaluationService.getSEvaluationById(evaluationId);
        restResult.setData(sEvaluation);
        restResult.setCode(CommonConstants.SUCCESS);
        return restResult;
    }

    @GetMapping("/getImproveDetailById/{evaluationId}")
    @ApiOperation(value = "绩效改进详情", notes = "绩效改进详情")
    public RestReturnResult<Object> getImproveDetailById(@PathVariable String evaluationId) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        ExtSEvaluation sEvaluation = sSuppliersEvaluationService.getImproveDetailById(evaluationId);
        restResult.setData(sEvaluation);
        restResult.setCode(CommonConstants.SUCCESS);
        return restResult;
    }

    @PostMapping("/submitImproveDetail")
    @ApiOperation(value = "绩效改进提交", notes = "绩效改进提交")
    public RestReturnResult<Object> submitImproveDetail(@RequestBody SEvaluaImproveVO vo) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        sSuppliersEvaluationService.submitImproveDetail(vo);
        return restResult;
    }

    @GetMapping("/getImproveHistory/{evaluationId}")
    @ApiOperation(value = "绩效改进历史", notes = "绩效改进提交")
    public RestReturnResult<Object> getImproveHistory(@PathVariable String evaluationId) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.getImproveHistory(evaluationId));
        return restResult;
    }

    @GetMapping("/getSEvaluationDealById/{evaluationId}")
    @ApiOperation(value = "供方评估打分代办详情接口", notes = "供方评估打分代办详情接口")
    public RestReturnResult<Object> getSEvaluationDealById(@PathVariable String evaluationId) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        ExtSEvaluation sEvaluation = sSuppliersEvaluationService.getSEvaluationDealById(evaluationId);
        restResult.setData(sEvaluation);
        restResult.setCode(CommonConstants.SUCCESS);
        return restResult;
    }

    @PostMapping("/getLaunchEvaluationData")
    @ApiOperation(value = "供方发起评估查询数据接口", notes = "供方发起评估查询数据接口")
    public RestReturnResult<Object> getLaunchEvaluationDataById(@RequestBody EvaluationLaunchDataRequest evaluationLaunchDataRequest) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        ExtSEvaluation sEvaluation = sSuppliersEvaluationService.getLaunchEvaluationData(evaluationLaunchDataRequest);
        restResult.setData(sEvaluation);
        restResult.setCode(CommonConstants.SUCCESS);
        return restResult;
    }

    @PostMapping("/getSEvaluationTemplates")
    @ApiOperation(value = "评估模板查询", notes = "评估模板查询")
    public RestReturnResult<List<SEvaluationTemplate>> getSEvaluationTemplateByEntity(@RequestBody SEvaluationTemplate sEvaluationTemplate) {
        RestReturnResult restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.getSEvaluationTemplateByEntity(sEvaluationTemplate));
        return restResult;
    }

    @GetMapping("/updateSEvaluationTemplates/{id}")
    @ApiOperation(value = "启用、停用模板", notes = "启用、停用模板")
    public RestReturnResult updateSEvaluationTemplates(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        sSuppliersEvaluationService.updateSEvaluationTemplates(id);
        return restResult;
    }

    @GetMapping("/deleteSEvaluationTemplates/{id}")
    @ApiOperation(value = "删除模板", notes = "删除模板")
    public RestReturnResult deleteSEvaluationTemplates(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        sSuppliersEvaluationService.deleteSEvaluationTemplates(id);
        return restResult;
    }

    @GetMapping("/getLaunchEvaluationTeml/{id}")
    @ApiOperation(value = "评估模板-发起评估选择模板后", notes = "评估模板查询-发起评估选择模板后")
    public RestReturnResult<ExtSEvaluation> getLaunchEvaluationTeml(@PathVariable String id) {
        RestReturnResult<ExtSEvaluation> restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.getLaunchEvaluationTeml(id));
        return restResult;
    }

    @GetMapping("/getOriEvaluationTeml/{id}")
    @ApiOperation(value = "评估模板-更新页面数据调用接口", notes = "评估模板-更新页面数据调用接口")
    public RestReturnResult<ExtSEvaluationTemplate> getOriEvaluationTeml(@PathVariable String id) {
        RestReturnResult<ExtSEvaluationTemplate> restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.getEvaluationTemlById(id));
        return restResult;
    }

    @PostMapping("/addOrUpdateLaunchEvaluationTeml")
    @ApiOperation(value = "评估模板-添加、修改评估模板", notes = "评估模板-添加、修改评估模板")
    public RestReturnResult<Object> addOrUpdateLaunchEvaluationTeml(@RequestBody ExtSEvaluationTemplate extSEvaluationTemplate) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        sSuppliersEvaluationService.saveEvaluationTeml(extSEvaluationTemplate);
        return restResult;
    }



    @PostMapping("/saveLaunchEvaluationData")
    @ApiOperation(value = "供方发起评估提交", notes = "供方发起评估提交")
    public RestReturnResult<Object> saveLaunchEvaluationData(@RequestBody ExtSEvaluation evaluationSaveLaunchDataRequest) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        sSuppliersEvaluationService.saveLaunchEvaluationData(evaluationSaveLaunchDataRequest);
        return restResult;
    }

    @PostMapping("/submitEvaluationScore")
    @ApiOperation(value = "待办打分", notes = "待办打分")
    public RestReturnResult<Object> submitEvaluationScore(@RequestBody ExtSEvaluation extSEvaluation) {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        sSuppliersEvaluationService.submitEvaluationScore(extSEvaluation);
        return restResult;
    }
    
    @PostMapping("/skipEvaluation")
    @ApiOperation(value = "跳过评估", notes = "履约过程评估")
    public RestReturnResult<Object> skipEvaluation(@RequestBody EvaluationSkipReqVO evaluationSkipReqVO){
    	RestReturnResult<Object> restResult = RestReturnResult.success();
    	sSuppliersEvaluationService.skipEvaluation(evaluationSkipReqVO);
    	return restResult;
    }

    @PostMapping("/supplierRankDtl")
    @ApiOperation(value = "评分定级详情", notes = "评分定级详情")
    public RestReturnResult<Object> supplierRankDtl(@RequestBody SupplierRankDtlReqVo vo){
        RestReturnResult<Object> restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.supplierRankDtl(vo));
        return restResult;
    }

    @PostMapping("/saveSupplierRankDtl")
    @ApiOperation(value = "评分定级详情保存", notes = "评分定级详情保存")
    public RestReturnResult<Object> saveSupplierRankDtl(@RequestBody SupplierRankDtlRspVO SupplierRankDtlRspVO) throws Exception {
        RestReturnResult<Object> restResult = RestReturnResult.success();
        sSuppliersEvaluationService.saveSupplierRankDtl(SupplierRankDtlRspVO);
        return restResult;
    }

    @PostMapping("/supplierRankScoreDtl")
    @ApiOperation(value = "工程评估打分代办详情", notes = "工程评估打分代办详情")
    public RestReturnResult<Object> supplierRankScoreDtl(@RequestBody SupplierRankDtlReqVo vo){
        RestReturnResult<Object> restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.supplierRankScoreDtl(vo));
        return restResult;
    }

    @PostMapping("/saveRankScores")
    @ApiOperation(value = "工程评估打分提交", notes = "工程评估打分提交")
    public RestReturnResult<Object> saveRankScores(@RequestBody SupplierRankDtlRspVO SupplierRankDtlRspVO) throws Exception{
        RestReturnResult<Object> restResult = RestReturnResult.success();
        sSuppliersEvaluationService.saveRankScores(SupplierRankDtlRspVO);
        return restResult;
    }

    @PostMapping("/supplierRanList")
    @ApiOperation(value = "评分定级列表", notes = "评分定级列表")
    public RestReturnResult<Object> supplierRanList(@RequestBody SupplierRankReqVO supplierRankReqVO){
        RestReturnResult<Object> restResult = RestReturnResult.success();
        restResult.setData(sSuppliersRankService.supplierRanList(supplierRankReqVO));
        return restResult;
    }

    @PostMapping("/launchSupplierRanList")
    @ApiOperation(value = "发起评分定级列表", notes = "发起评分定级列表")
    public RestReturnResult<Object> launchSupplierRanList(@RequestBody SupplierRankReqVO supplierRankReqVO){
        RestReturnResult<Object> restResult = RestReturnResult.success();
        restResult.setData(sSuppliersRankService.launchSupplierRanList(supplierRankReqVO));
        return restResult;
    }

    @PostMapping("/rankHistoryList")
    @ApiOperation(value = "评分定级审批历史", notes = "评分定级审批历史")
    public RestReturnResult<Object> rankHistoryList(@RequestBody RankHistoryReqVO rankHistoryReqVO){
        RestReturnResult<Object> restResult = RestReturnResult.success();
        restResult.setData(sHistoryCommentService.historyCommentList(rankHistoryReqVO));
        return restResult;
    }


    @PostMapping("/initiateApprovalSupplierRank")
    @ApiOperation(value = "发起定级审批", notes = "发起定级审批")
    public RestReturnResult initiateApprovalSupplierRank(@RequestBody InitiateApprovalSupplierRankReqVo vo){
        RestReturnResult restResult = RestReturnResult.success();
        String mbsUrl = sSuppliersEvaluationService.initiateApprovalSupplierRank(vo);
        restResult= new RestReturnResult<Object>(CommonConstants.SUCCESS, mbsUrl ,null,"操作成功");
        return restResult;
    }

    @GetMapping("/approvalSupplierRankDtl/{id}")
    @ApiOperation(value = "定级审批详情", notes = "定级审批详情")
    public RestReturnResult approvalSupplierRankDtl(@PathVariable String id){
        RestReturnResult restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.approvalSupplierRankDtl(id));
        return restResult;
    }

    @PostMapping("/selectIdAndNameByEntity")
    @ApiOperation(
            value = "查询供方",
            notes = "查询供方",
            response = SSuppliers.class
    )
    public RestReturnResult selectSuppliers(@RequestBody RankHistoryReqVO vo
    ) throws Exception {
        RestReturnResult restReturnResult = RestReturnResult.success();
        List<SSuppliers> supplieIDandnameVos = sSuppliersService.selectRankable(vo);
        restReturnResult.setData(supplieIDandnameVos);
        return restReturnResult;
    }

    @PostMapping("/approvalSupplierRank")
    @ApiOperation(value = "定级过程审批接口", notes = "定级过程审批接口")
    public RestReturnResult approvalSupplierRank(@RequestBody SupplierRankApprovalResultVo vo){
        RestReturnResult restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.approvalSupplierRank(vo));
        return restResult;
    }

    @PostMapping("/approvalSupplierRankFinal")
    @ApiOperation(value = "定级最终审批", notes = "定级审批接口")
    public RestReturnResult approvalSupplierRankFinal(@RequestBody SupplierRankApprovalResultVo vo){
        RestReturnResult restResult = RestReturnResult.success();
        sSuppliersEvaluationService.approvalSupplierRankFinal(vo);
        return restResult;
    }


    @GetMapping("/selectByName/{name}")
    @ApiOperation(
            value = "根据名称查询",
            notes = "获取所有mbs合同数据接口",
            response = SSuppliersMbsContacts.class
    )
    public RestReturnResult selectByName(@PathVariable String name) {
        RestReturnResult restResult = RestReturnResult.success();
        restResult.setData(sSuppliersEvaluationService.findSimByname(name));
        return restResult;
    }


    @PostMapping("/selectContactList")
    @ApiOperation(
            value = "查询后评估合同列表",
            notes = "查询后评估合同列表",
            response = SSuppliersMbsContacts.class
    )
    public RestReturnResult selectContactList(@RequestBody EvaluaConcatParamVo vo) {
        RestReturnResult restResult = RestReturnResult.success();
        Page<ExtSSuppliersLastThreeAchieve> page = new Page<>();
        page.setCurrent(vo.getPageRequestVO().getPageNumber());
        page.setSize(vo.getPageRequestVO().getPageSize());
        page = sSuppliersEvaluationService.selectEvaluationLastContact(page,vo.getExtSSuppliersLastThreeAchieve());
        restResult.setData(page);
        return restResult;
    }

}
